import { PlayerState } from "../GlobalData/DataStruct";
import DataCenter from "../GlobalData/DataCenter";

export default class player_controller extends Laya.Script {
    
    constructor() { super(); }
    
    public player_ani:Laya.Animation           //玩家动画节点
    public player_state:PlayerState           //玩家动画状态
    public player_id:any                      //玩家编号

    private isPlayer:boolean                  //是否是玩家
    private box_collider:Laya.BoxCollider     //盒子碰撞器
    private rigid_body:Laya.RigidBody         //刚体组件
    private name_text:Laya.Text               //名字文本

    /**
     * 初始化玩家
     * @param x 坐标
     * @param y 坐标
     * @param node 节点
     * @param isPlayer 是否是当前玩家
     */
    public initPlayer(x:number,y:number,node:Laya.Node,isPlayer:boolean,label:string,name:string,id:number)
    {
        //设置玩家id
        this.player_id=id
        //设置是否为当前玩家
        this.isPlayer=isPlayer
        //初始化动画
        this.player_ani=new Laya.Animation()
        //加载图集动画
        this.player_ani.loadAtlas("player_skin/idel.atlas",null,"idel")
        this.player_ani.loadAtlas("player_skin/win.atlas",null,"win")
        this.player_ani.loadAtlas("player_skin/run.atlas",null,"run")
        
        //添加BoxCollider组件
        this.box_collider=this.player_ani.addComponent(Laya.BoxCollider)
        //添加标签（label）
        this.box_collider.label=label
        //添加刚体组件
        this.rigid_body=this.player_ani.addComponent(Laya.RigidBody)
        //设置重力系数
        this.rigid_body.gravityScale=0
        //设置刚体类型
        this.rigid_body.type="kinematic"
        //设置不能够旋转
        this.rigid_body.allowRotation=false
        //添加到舞台
        node.addChild(this.player_ani)
        //设置初始位置
        this.player_ani.pos(x,y)
        //设置人物大小比例
        this.player_ani.scale(0.8,0.8)
        //播放默认动画
        this.player_ani.play(0,true,"idel")
        //更改玩家动画状态
        this.player_state=PlayerState.IDEL

        //设置人物名字
        this.name_text=new Laya.Text()
        this.name_text.text=name
        this.name_text.fontSize=30
        this.name_text.bold=true
        this.player_ani.addChild(this.name_text)
        this.name_text.pos(25,-20)


    }

    /**玩家移动 */
    public Move()
    {
        if(this.player_state==PlayerState.IDEL)
        {
            //播放run动画
            this.player_ani.play(0,true,"run")
            //更改玩家动画状态
            this.player_state=PlayerState.RUN
            //当玩家x坐标小于等于500时才移动位置
            if(this.player_ani.x<=500||!this.isPlayer)
            {
                //播放缓动动画实现移动
                Laya.Tween.to( 
                    this.player_ani,
                    {x:this.player_ani.x+20},
                    200,
                    null,
                    Laya.Handler.create(this,function(){
                        //将动画切换为idel状态
                        this.player_ani.play(0,true,"idel")
                        //更改玩家动画状态
                        this.player_state=PlayerState.IDEL
                        //如果不是玩家就不加上20
                        this.isPlayer?
                        DataCenter.player_distance+=20 :
                        DataCenter.player_distance
                    })
                )
            }
            
        }   
    }

    onDisable(): void {
    }
}